package num49;

import java.util.*;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/8 19:49
 * @Desc :
 */
public class Solution {
  public List<List<String>> groupAnagrams(String[] strs) {
    HashMap<String, List<String>> map = new HashMap<>();
    for (String str : strs) {
      char[] array = str.toCharArray();
      Arrays.sort(array);
      String key = new String(array);
      List<String> list = map.get(key);
      if (list == null)
        map.put(key, new ArrayList<>(Collections.singletonList(str)));
      else
        list.add(str);
    }
    return new ArrayList<>(map.values());
  }

  public static void main(String[] args) {
    String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    List<List<String>> res = new Solution().groupAnagrams(strs);
    System.out.println(res);
  }

}
